﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using StockholmsStad.Windows.UI.Modules.Maintenance.Interfaces;
using StockholmsStad.Windows.UI.Modules.Maintenance.Models;
using StockholmsStad.Runtime.Serialization.DataContract;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Views
{
    /// <summary>
    /// Interaction logic for TabItemInformationTextView.xaml
    /// </summary>
    public partial class TabItemInformationTextView : IInformationTextView
    {
        private readonly IInformationTextService service;
        private ValueIdPair selectedTextType;
        private InformationText selectedText;

        public TabItemInformationTextView(IInformationTextService service) 
        {
            InitializeComponent();

            this.service = service;

            Loaded += TabItemInformationTextView_Loaded;
            cbTexts.SelectionChanged += cbTexts_SelectionChanged;
        }
       
        private void TabItemInformationTextView_Loaded(object sender, RoutedEventArgs e)
        {
            Model = service.GetDefaultPresentationModel();

            rbFaq.IsChecked = true;
            SetTextType(Model.TextTypes[0]);
            
            ControlMode = ViewMode.View;
        }

        private void checkBox_Click(object sender, RoutedEventArgs e)
        {
            int index = rbFaq.IsChecked == true ? 0 : 1;
            SetTextType(Model.TextTypes[index]);
        }

        private void SetTextType(ValueIdPair type)
        {
            selectedTextType = type;

            Model.Texts = service.GetInformationTextsFiltered(selectedTextType.Id);
            cbTexts.DataContext = Model.Texts;

            if (Model.Texts.Count > 0)
                cbTexts.SelectedIndex = 0;
        }

        private void cbTexts_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selectedText = cbTexts.SelectedItem as InformationText;

            if (selectedText != null)
            {
                tbHeader.Text = selectedText.Header;
                tblContent.Text = selectedText.Content;
                tbContent.Text = selectedText.Content;
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            tbHeader.Text = "";
            tbContent.Text = "";

            ControlMode = ViewMode.EditNew;
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            selectedText = cbTexts.SelectedItem as InformationText;
            ControlMode = ViewMode.EditUpdate;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (ControlMode == ViewMode.EditNew)
            {
                selectedText = new InformationText
                                   {
                    TextId = null,
                    TextType = selectedTextType,
                    Header = tbHeader.Text,
                    Content = tbContent.Text
                };

                selectedText.TextId = service.AddInformationText(selectedText);
                Model.AddText(selectedText);
            }
            else //if(ControlMode == ViewMode.EditUpdate)
            {
                selectedText.Header = tbHeader.Text;
                selectedText.Content = tbContent.Text;

                service.UpdateInformationText(selectedText);
            }

            InformationText item = (from p in Model.Texts
                            where p.TextId == selectedText.TextId
                            select p).Single();

            var index = Model.Texts.IndexOf(item);

            //int index = Model.Texts.IndexOf(selectedText);
            cbTexts.SelectedIndex = -1;
            cbTexts.SelectedIndex = index;

            //cbTexts.ItemsSource = Model.Texts;
            //cbTexts.SelectedItem = selectedText;
            tblContent.Text = selectedText.Content;

            ControlMode = ViewMode.View;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            int index = 0;

            if (selectedText != null)
            {
                index = cbTexts.Items.IndexOf(selectedText);

                service.DeleteInformationText(selectedText);
                Model.Texts.Remove(selectedText);
            }

            if (index > 0)
            {
                index--;
                cbTexts.SelectedIndex = index;
                selectedText = cbTexts.SelectedItem as InformationText;
            }
            else 
            {
                cbTexts.SelectedItem = null;
                selectedText = null;
            }

            ControlMode = ViewMode.View;
        }

        private void btnUndo_Click(object sender, RoutedEventArgs e)
        {
            if (selectedText == null)
            {
                tbHeader.Text = String.Empty;
                tbContent.Text = String.Empty;
            }
            else 
            {
                tbHeader.Text = selectedText.Header;
                tbContent.Text = selectedText.Content;
            }

            ControlMode = ViewMode.View;

            //if (Model.SelectedInformationText != null)
            //{
            //    var v = from p in Model.Texts
            //            where p.TextId == Model.SelectedInformationText.TextId
            //            select p;

            //    InformationText text = Model.SelectedInformationText;
            //    tbHeader.Text = text.Header;
            //    tblContent.Text = text.Content;
            //    tbContent.Text = text.Content;
            //}
            //else 
            //{
            //    tbHeader.Text = String.Empty;
            //    tblContent.Text = String.Empty;
            //    tbContent.Text = String.Empty;
            //}
        }

        private ViewMode controlMode;
        private ViewMode ControlMode 
        {
            get { return controlMode; }
            set
            {
                controlMode = value;
                RefreshControls();
            }
        }

        private void RefreshControls()
        {
            if (ControlMode == ViewMode.View)
            {
                btnAdd.Visibility = Visibility.Visible;
                btnEdit.Visibility = cbTexts.SelectedItem != null ? Visibility.Visible : Visibility.Collapsed;
                btnSave.Visibility = Visibility.Collapsed;
                btnDelete.Visibility = cbTexts.SelectedItem != null ? Visibility.Visible : Visibility.Collapsed;
                btnUndo.Visibility = Visibility.Collapsed;

                rbFaq.IsEnabled = true;
                rbLawText.IsEnabled = true;

                tblContent.Visibility = Visibility.Visible;
                tbContent.Visibility = Visibility.Collapsed;
                cbTexts.Visibility = Visibility.Visible;
                tbHeader.Visibility = Visibility.Collapsed;
            }
            else //if(ControlMode == ViewMode.EditNew || ControlMode == ViewMode.EditUpdate)
            {
                btnAdd.Visibility = Visibility.Collapsed;
                btnEdit.Visibility = Visibility.Collapsed;
                btnSave.Visibility = Visibility.Visible;
                btnDelete.Visibility = Visibility.Collapsed;
                btnUndo.Visibility = Visibility.Visible;

                rbFaq.IsEnabled = false;
                rbLawText.IsEnabled = false;

                tblContent.Visibility = Visibility.Collapsed;
                tbContent.Visibility = Visibility.Visible;
                cbTexts.Visibility = Visibility.Collapsed;
                tbHeader.Visibility = Visibility.Visible;
            }
        }

        public InformationTextPresentationModel Model
        {
            get { return DataContext as InformationTextPresentationModel; }
            set { DataContext = value; }
        }

        private enum ViewMode
        {
            View,
            EditNew,
            EditUpdate
        }
    }
}
